<?php
/**
 * Scheduler module for Joomla! 1.5
 * @author      DHTMLX, <dhtmlx.gmail.com>
 * @package		scheduler
 * @subpackage	scheduler
 * @version     1.0.0
 * @copyright   Copyright (C) 2009 DHTML LTD , http://dhtmlx.com
 * @license     http://www.gnu.org/licenses/gpl.html GNU/GPL
 */
	define( '_JEXEC', 1 );
	define( 'DS', DIRECTORY_SEPARATOR );
	define('JPATH_BASE', dirname(__FILE__).DS.'..'.DS.'..');
	require_once ( JPATH_BASE .DS.'configuration.php' );
	require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
	require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
	$mainframe =& JFactory::getApplication ('site');
	$mainframe->initialise ();
	$mainframe->route ();
	
	$usertype = getUser();
	
	$cfg = new JConfig;
	$host = $cfg->host;
	$user = $cfg->user;
	$pass = $cfg->password;
	$db = $cfg->db;
	$prefix = $cfg->dbprefix;
	
	
	include ('.'.DS.'codebase'.DS.'connector'.DS.'scheduler_connector.php');

	$res=mysql_connect($host,$user,$pass);
	mysql_select_db($db);
	
	$opt_res = mysql_query("SELECT `name`, `value` FROM `".$prefix."scheduler_options`");
	$options = Array();
	while ($row = mysql_fetch_assoc($opt_res)) {
		$options[$row['name']] = $row['value'];
	}

	$table_name = $prefix.$options['table_name'];

	function getUser() {
		$user =& JFactory::getUser();
		switch($user->usertype) {
			case '':
				$usertype = 'guests';
				break;
			case 'Super Administrator':
				$usertype = 'super_administrator';
				break;
			default:
				$usertype = strtolower($user->usertype);
			}
		return $usertype;
		}
	
	
	function delete_related($action){
		global $scheduler, $table_name;
		
		$status = $action->get_status();
		$type =$action->get_value("rec_type");
		$pid =$action->get_value("event_pid");
		if (($status == "deleted" || $status == "updated") && $type!=""){
			$scheduler->sql->query("DELETE FROM `".$table_name."` WHERE event_pid='".$scheduler->sql->escape($action->get_id())."'");
		}
		if ($status == "deleted" && $pid !=0){
			$scheduler->sql->query("UPDATE `".$table_name."` SET rec_type='none' WHERE event_id='".$scheduler->sql->escape($action->get_id())."'");
			$action->success(); 
		}
	
	}
	function insert_related($action){
		$status = $action->get_status();
		$type =$action->get_value("rec_type");
		
		if ($status == "inserted" && $type=="none")
			$action->set_status("deleted");
	}
	$scheduler = new schedulerConnector($res);
	
	if ($options['debug'] == '1') {
		$scheduler->enable_log("scheduler.log", true);
	}
	
	if ($options['repeat'] == '1') {
		$scheduler->event->attach("beforeProcessing","delete_related");
		$scheduler->event->attach("afterProcessing","insert_related");
		}

	if ($options[$usertype] == '0') {
		$scheduler->access->deny("update");
		$scheduler->access->deny("insert");
		$scheduler->access->deny("delete");
		}
	
	$config =& JFactory::getConfig();
	if ($config->_registry['config']['data']->scheduler_log == true) {
		$scheduler->enable_log(JPATH_ROOT."/scheduler.log");
	}
	$scheduler->render_table($table_name,"event_id","start_date,end_date,text,rec_type,event_pid,event_length");

?>